package com.px.user.service;

import com.google.common.collect.ImmutableMap;
import com.px.user.mapper.CheatMapper;
import com.px.user.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class TeacherService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CheatMapper cheatMapper;

    public void courseDelete(Integer id, Authentication authentication) {
        Collection roles = authentication.getAuthorities();
        SimpleGrantedAuthority admin = new SimpleGrantedAuthority("ADMIN");
        if (roles.contains(admin))
            courseMapper.delete(id);
        else
            courseMapper.deleteByCreatorId(ImmutableMap.of("id", id, "creatorId", authentication.getName()));
    }

    public void cheatDelete(Integer id, Authentication authentication) {
        Collection roles = authentication.getAuthorities();
        SimpleGrantedAuthority admin = new SimpleGrantedAuthority("ADMIN");
        if (roles.contains(admin))
            cheatMapper.delete(id);
        else
            cheatMapper.deleteByCreatorId(ImmutableMap.of("id", id, "creatorId", authentication.getName()));
    }
}
